package com.mydigitallife.timetracker;

import android.app.Activity;
import android.database.*;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.DatePicker.OnDateChangedListener;

public class changeData extends Activity{
	private String end = null; 
	private String start = null;
	public void onCreate(Bundle savedInstanceState) {
		  super.onCreate(savedInstanceState);
		  setContentView(R.layout.changedata);


		  
		  Bundle dataTransfer = getIntent().getExtras();
		  int dataId = dataTransfer.getInt("test");
			
		  EditText projectNameField = (EditText) findViewById(R.id.changeData);

		  Cursor selectprojectname = connectDB().rawQuery("SELECT projects._id, projects.projectname FROM projects, tracking ON tracking._id = projects._id WHERE tracking.trackid = "+dataId+";", null);
		  
		  String projectName = null;
		  if(selectprojectname.moveToFirst()){
			do{
				projectName = selectprojectname.getString(1);
			} while(selectprojectname.moveToNext());
		  }
		  projectNameField.setText(projectName);
		  checkStarttimeClick();
		  checkStartdateClick();
		  checkEnddateClick();
		  checkEndtimeClick();
		  
		  enterDatesAndTimes(dataId);
		  connectDB().close();
	}
	
	private void checkStartdateClick(){
		Button startDateButton = (Button) findViewById(R.id.startDateButton);
		startDateButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				DatePicker startDatePicker = (DatePicker) findViewById(R.id.startDatePicker);

				if(startDatePicker.getVisibility() == 8){
					startDatePicker.setVisibility(0);
				}else{
					startDatePicker.setVisibility(View.GONE);
				}
			}
		});
	}
	
	private void checkStarttimeClick(){
		Button startTimeButton = (Button) findViewById(R.id.startTimeButton);
		startTimeButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				TimePicker startTimePicker = (TimePicker) findViewById(R.id.startTimePicker);
				
				if(startTimePicker.getVisibility() == 8){
					startTimePicker.setVisibility(0);
				}else{
					startTimePicker.setVisibility(View.GONE);
				}
				
			}
		});
	}
	
	private void checkEnddateClick(){
		Button endDateButton = (Button) findViewById(R.id.endDateButton);
		endDateButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				DatePicker endDatePicker = (DatePicker) findViewById(R.id.endDatePicker);
				
				if(endDatePicker.getVisibility() == 8){
					endDatePicker.setVisibility(0);
				}else{
					endDatePicker.setVisibility(View.GONE);
				}
			}
		});
	}
	
	private void checkEndtimeClick(){
		Button endTimeButton = (Button) findViewById(R.id.endTimeButton);
		endTimeButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				TimePicker endTimePicker = (TimePicker) findViewById(R.id.endTimePicker);

				if(endTimePicker.getVisibility() == 8){
					endTimePicker.setVisibility(0);
				}else{
					endTimePicker.setVisibility(View.GONE);
				}
			}
		});
	}
	
	private void enterDatesAndTimes(final int trackId){

		// Set pickers
		TimePicker startTimePicker = (TimePicker) findViewById(R.id.startTimePicker);
		DatePicker startDatePicker = (DatePicker) findViewById(R.id.startDatePicker);

		start = getCurrentStart(trackId);
		
		startTimePicker.setCurrentHour(Integer.parseInt(start.substring(11,13)));
		startTimePicker.setCurrentMinute(Integer.parseInt(start.substring(14,16)));
		System.out.println(Integer.parseInt(start.substring(0, 4))+"-"+ start.substring(5,7) + "-"+ Integer.parseInt(start.substring(8,10)));
		startTimePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
			
			@Override
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				start = getCurrentStart(trackId);
				System.out.println("start Time + date changed:");
				String minuteString = null;
				if(Integer.toString(minute).length() == 1){
					minuteString = 0+""+Integer.toString(minute);
				}else {
					minuteString = Integer.toString(minute);
				}
				String hourString = null;
				if(Integer.toString(hourOfDay).length() == 1){
					hourString = 0+""+Integer.toString(hourOfDay);
				} else {
					hourString = Integer.toString(hourOfDay);
				}
				
				System.out.println( start.substring(0, 4)+"-"+ start.substring(5,7) + "-"+ start.substring(8,10) +" "+hourString+":"+minuteString+":00");
				String changedTime = Integer.parseInt(start.substring(0, 4))+"-"+ start.substring(5,7) + "-"+ start.substring(8,10) +" "+hourString+":"+minuteString+":00";
				changeData("starttime", changedTime, trackId);
			}
		});
		
		startDatePicker.init(Integer.parseInt(start.substring(0, 4)) , Integer.parseInt(start.substring(5,7))+1 , Integer.parseInt(start.substring(8,10)) , new OnDateChangedListener() {
			
			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				start = getCurrentStart(trackId);
				System.out.println("start Time + date changed:");
				String dayString = null;
				if(Integer.toString(dayOfMonth).length() == 1 ){
				 dayString = 0+""+Integer.toString(dayOfMonth);
				}else{
					dayString = Integer.toString(dayOfMonth);
				}
				String monthOfYearString = null;
				if(Integer.toString(monthOfYear).length() == 1 ){
					monthOfYearString = 0+""+Integer.toString(monthOfYear);
				}else{
					monthOfYearString = Integer.toString(monthOfYear);
				}
				System.out.println(year+"-"+monthOfYearString+"-"+dayString+" " + Integer.parseInt(start.substring(11,13))+":" + Integer.parseInt(start.substring(14,16)) + ":00");
				String changedDate =  year+"-"+monthOfYearString+"-"+dayString+" " + Integer.parseInt(start.substring(11,13))+":" + Integer.parseInt(start.substring(14,16)) + ":00";
				changeData("starttime", changedDate, trackId);
			}
		});
		
		TimePicker endTimePicker = (TimePicker) findViewById(R.id.endTimePicker);
		DatePicker endDatePicker = (DatePicker) findViewById(R.id.endDatePicker);
		
		end = getCurrentEnd(trackId);
		
		endTimePicker.setCurrentHour(Integer.parseInt(end.substring(11,13)));
		endTimePicker.setCurrentMinute(Integer.parseInt(end.substring(14,16)));
		endTimePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
			
			@Override
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				end = getCurrentEnd(trackId);
				String minuteString = null;
				if(Integer.toString(minute).length() == 1){
					minuteString = 0+""+Integer.toString(minute);
				}else {
					minuteString = Integer.toString(minute);
				}
				String hourString = null;
				if(Integer.toString(hourOfDay).length() == 1){
					hourString = 0+""+Integer.toString(hourOfDay);
				} else {
					hourString = Integer.toString(hourOfDay);
				}
				
				System.out.println("End Time + date changed:");
				System.out.println(Integer.parseInt(end.substring(0, 4))+"-"+ Integer.parseInt(end.substring(5,7)) + "-"+ Integer.parseInt(end.substring(8,10)) +" "+hourString+":"+minuteString+":00");
				String changedTime = Integer.parseInt(end.substring(0, 4))+"-"+ end.substring(5,7) + "-"+ end.substring(8,10) +" "+hourString+":"+minuteString+":00";
				changeData("endtime", changedTime, trackId);
			}
		});
		endDatePicker.init(Integer.parseInt(end.substring(0, 4)), Integer.parseInt(end.substring(5,7)), Integer.parseInt(end.substring(8,10)), new OnDateChangedListener() {
			
			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				end = getCurrentEnd(trackId);
				System.out.println("End Time + date changed:");
				String dayString = null;
				System.out.println(Integer.toString(monthOfYear).length());
				if(Integer.toString(dayOfMonth).length() == 1 ){
				 dayString = 0+""+Integer.toString(dayOfMonth);
				}else{
					dayString = Integer.toString(dayOfMonth);
				}
				String monthOfYearString = null;
				if(Integer.toString(monthOfYear).length() == 1 ){
					monthOfYearString = 0+""+Integer.toString(monthOfYear);
				}else{
					monthOfYearString = Integer.toString(monthOfYear);
				}
				
				System.out.println(year+"-"+monthOfYearString+"-"+dayString+" " + Integer.parseInt(end.substring(11,13))+":" + Integer.parseInt(end.substring(14,16)) + ":00");
				String changedDate =  year+"-"+monthOfYearString+"-"+dayString+" " + Integer.parseInt(end.substring(11,13))+":" + Integer.parseInt(end.substring(14,16)) + ":00";
				changeData("endtime", changedDate, trackId);
			}
		});
	}
	
	public SQLiteDatabase connectDB(){
		SQLiteDatabase myDB = null;
		myDB = this.openOrCreateDatabase("TimeTrack", MODE_PRIVATE, null);
		return myDB;
	}
	
	private void changeData(String updateField,String data, int dataId){
		connectDB().execSQL("UPDATE tracking SET " + updateField + "='"+data+"'  WHERE trackid = "+ dataId);
	}
	
	private String getCurrentStart(int trackId){
		Cursor startCursor = connectDB().rawQuery("SELECT starttime FROM tracking WHERE trackid="+trackId+";",null);
		// set format 
		String starting = null;
		if(startCursor.moveToFirst()){
			starting = startCursor.getString(0);
		}
		return starting;
	}
	
	private String getCurrentEnd(int trackId){
		Cursor endTimeCursor = connectDB().rawQuery("SELECT endtime FROM tracking WHERE trackid="+trackId+";",null);
		
		String selectedDate = null;

		if(endTimeCursor.moveToFirst()){			
			selectedDate = endTimeCursor.getString(0);
		}
		
		
		return selectedDate;
	}
}
